package com.gla.market.adapter;


import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.gla.market.R;
import com.gla.market.activity.UpdateAddressActivity;
import com.gla.market.entity.AddressItem;


import java.util.List;

public class ReceivingAddressAdapter extends BaseAdapter {
    private Context context;
    private List<AddressItem> addressItemList;//数据集合

    public ReceivingAddressAdapter(Context context, List<AddressItem> addressItemList) {
        this.context = context;
        this.addressItemList = addressItemList;
    }

    @Override
    public int getCount() {
        return addressItemList.size();//listview中有多少个itemview
    }

    @Override
    public Object getItem(int position) {
        return addressItemList.get(position);//返回每个item的对象
    }

    @Override
    public long getItemId(int positon) {
        return positon;//返回item的位置
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        int type = getItemViewType(position);//判断是哪种类型

        if (convertView == null){//为空时构造布局文件
            holder = new ViewHolder();
            switch (type){
                case 1:
                    //(独有的赋值)

                    break;
            }
            //（共有的赋值）

            //将xml布局转换为view
            convertView = LayoutInflater.from(context).inflate(R.layout.address_list,parent,false);
            //给viewholder的变量赋值
            holder.shouhuo_name = convertView.findViewById(R.id.shouhuo_name);
            holder.shouhuo_phone = convertView.findViewById(R.id.shouhuo_phone);
            holder.shouhuo_address = convertView.findViewById(R.id.shouhuo_address);
            holder.shouhuo_photo = convertView.findViewById(R.id.shouhuo_photo);
            holder.shouhuo_edit = convertView.findViewById(R.id.shouhuo_edit);
            holder.shouhuo_default = convertView.findViewById(R.id.shouhuo_default);
            //将viewholder缓存
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        AddressItem addressItem = addressItemList.get(position);
        holder.shouhuo_name.setText(addressItem.getGetman());
        holder.shouhuo_phone.setText(addressItem.getPhone());
        holder.shouhuo_address.setText(addressItem.getArea()+addressItem.getName());
        if(addressItem.getIs_default()==1){
            holder.shouhuo_default.setVisibility(View.VISIBLE);
        } else {
            holder.shouhuo_default.setVisibility(View.GONE);
        }
        switch (type){
            case 1:
                break;
        }
        holder.shouhuo_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, UpdateAddressActivity.class);
                intent.putExtra("userAddress",addressItem);
                context.startActivity(intent);
            }
        });
        return convertView;
    }
    //获取“添加收货地址”布局页面的RecyclerView的子项
    @Override
    public int getItemViewType(int position){
        return addressItemList.get(position).getItemType();
    }
    //item的类型数量
    @Override
    public int getViewTypeCount(){
        return 2;
    }
    public class ViewHolder {
        TextView shouhuo_name,shouhuo_phone,shouhuo_address;
        ImageView shouhuo_photo;
        ImageButton shouhuo_edit;
        TextView shouhuo_default;
    }

}
